package com.vn.egame.elazylite.utils;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo.DetailedState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;

import com.egame.ewidget.R;

public class Utils {
	public static String getWifiName(Context context) {
		String name = context.getResources().getString(R.string.util_wifi);
		WifiManager manager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		if (manager.isWifiEnabled()) {
			WifiInfo wifiInfo = manager.getConnectionInfo();
			if (wifiInfo != null) {
				DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo
						.getSupplicantState());
				if (state == DetailedState.CONNECTED
						|| state == DetailedState.OBTAINING_IPADDR) {
					name = wifiInfo.getSSID();

					if (name == null || name.length() == 0)
						return context.getResources().getString(
								R.string.util_wifi);

					for (int i = 0; i < name.length(); i++) {

						if ((int) name.charAt(i) == 34) {

							name = name.replace("\"", "");

						}
					}

					if (name == null || name.length() == 0)
						return context.getResources().getString(
								R.string.util_wifi);
					return name;
				}
			}
		}
		return name;
	}

	public static int batteryLevel(Context context) {
		int percent;
		try {
			Intent intent = context.registerReceiver(null, new IntentFilter(
					Intent.ACTION_BATTERY_CHANGED));
			int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
			int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
			percent = (level * 100) / scale;
		} catch (Exception ex) {
			return 100;
		}
		return percent; // String.valueOf(percent) + "%";
	}
}
